<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生管理系统注册</title>
    <link rel="stylesheet" href="./css/login.css">
    <link rel="shortcut icon" href="./images/logo.ico" type="image/x-icon">
</head>
<body>
    <form action="" class="formEle">
        <div class="login-reg">
            <strong>注册</strong>
            <a href="./login.html">登录</a>
        </div>
        <div>
            <label for="username">用户名：</label>
            <input type="text" id="username" placeholder="请输入用户名">
        </div>
        <div>
            <label for="account">账号：</label>
            <input type="text" id="account" placeholder="请输入账号">
        </div>
        <div>
            <label for="password">密码：</label>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <div>
            <label for="passwordAgain">确认密码：</label>
            <input type="password" id="passwordAgain" placeholder="请输入密码">
        </div>

        <div>
            <button type="submit">注册</button>
            <button type="reset">重置</button>
        </div>
    </form>

    <script src="./js/ajax.js"></script>
    <script>
        var obj = {
            form: document.getElementsByClassName('formEle')[0],
            init: function() {
                this.eventAdd();
            },
            eventAdd: function() {
                var that = this;
                var submitBtn = this.form.getElementsByTagName('button')[0];
                submitBtn.onclick = function(e) {
                    var event = e || window.event;
                    e.preventDefault();
                    var username = that.form.username.value;
                    // console.log(username);
                    var account = that.form.account.value;
                    var password = that.form.password.value;
                    var passwordAgain = that.form.passwordAgain.value;

                    if(username && account && password && passwordAgain) {
                        if(password !== passwordAgain) {
                            alert('两次输入密码不一样！');
                        }else{
                            var data = 'appkey=xiaobaibai_1590807635908&account=' + account + '&username=' + username + '&password=' + password + '&rePassword=' + passwordAgain;
                            ajax('POST', 'http://open.duyiedu.com/api/student/stuRegister', data, function(res){
                                console.log(res);
                                if(res.status === 'success') {
                                    alert('注册成功，去登陆！');
                                    window.location.href = './login.html';
                                }else{
                                    alert(res.msg);
                                }
                            }, true);
                        }

                    }else{
                        alert('表单填写不完整。。');
                    }

                }
            }
        }
        obj.init();

    </script>

</body>
</html>